package com.oocl.crm.controller.impl;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;

import com.oocl.crm.controller.Controller;
import com.oocl.crm.customer.Customer;
import com.oocl.crm.db.MyLinkedList;
import com.oocl.crm.parser.CmdParser;
import com.oocl.crm.parser.impl.CmdParserImpl;
import com.oocl.crm.util.Add;
import com.oocl.crm.util.FormatPrinter;

public class ControllerImpl implements Controller{
	private CmdParser parser;
	private MyLinkedList<Customer> mll;
	
	public ControllerImpl(MyLinkedList<Customer> mll){
		this.mll=mll;
	}
	
	@Override
	public boolean commandProcess(InputStream in, OutputStream out) {
		//完成键盘命令输入接收
		Scanner scaner=new Scanner(System.in);
		String cmd=scaner.nextLine();
		//初步解析，并调用CmdParser
		parser=new CmdParserImpl(mll);
		//调用FomartPrinter打印输出
		String action=null;
		String[] str1;
		str1=cmd.split(" ");
		action=str1[0];
		Customer[] cs;
		Customer c;
		switch (action) {
		case "L":
			cs= parser.find(cmd);
			FormatPrinter.printCustomers(cs, null);
			break;
		case "A":
			parser.processAdd(cmd);
			break;
		case "D":
			parser.processDelete(cmd);
			break;
		case "U":
			parser.processUpdate(cmd);
			break;
		case "O":
			cs=parser.processSort(cmd);
			FormatPrinter.printCustomers(cs, null);
			break;
		case "H":
			FormatPrinter.printHelpDoc(cmd, null);
			break;
		case "Q":
			return false;
		default:
			break;
		}
		return true;
	}
}
